package com.kaizeli.website.config;

import com.kaizeli.website.interceptor.AdminAuthInterceptor;
import com.kaizeli.website.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类
 * 配置拦截器、CORS等
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;
    
    @Autowired
    private AdminAuthInterceptor adminAuthInterceptor;

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 1. JWT拦截器：验证用户登录状态
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns(
                        "/api/**",           // 拦截所有API请求
                        "/ws-debug.html"     // 拦截WebSocket调试页面，需要登录
                )
                .excludePathPatterns(
                        // 用户认证相关（不需要Token）
                        "/api/user/login",
                        "/api/user/validate",
                        
                        // 验证码相关（不需要Token）
                        "/api/captcha/**",
                        
                        // Knife4j文档相关（不需要Token）
                        "/doc.html",
                        "/favicon.ico",
                        "/webjars/**",
                        "/swagger-resources/**",
                        "/v3/api-docs/**",
                        
                        // 静态资源（不需要Token）
                        "/static/**",
                        "/css/**",
                        "/js/**",
                        "/images/**"
                );
        
        // 2. 管理员权限拦截器：验证管理员权限
        registry.addInterceptor(adminAuthInterceptor)
                .addPathPatterns(
                        // 用户创建（只有管理员可以创建用户）
                        "/api/user/create",
                        
                        // 数据导入接口（只有管理员可以导入数据）
                        "/api/project/import",
                        "/api/project/import-update",
                        "/api/employee-active/import",
                        "/api/employee-active/import-update",
                        "/api/employee-resigned/import",
                        "/api/employee-resigned/import-update",
                        "/api/contract-personnel/import",
                        "/api/contract-personnel/import-update"
                );
    }

    /**
     * 配置静态资源处理
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Knife4j文档资源
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        
        registry.addResourceHandler("/swagger-resources/**")
                .addResourceLocations("classpath:/META-INF/resources/");
        
        // 静态资源 - 支持根路径直接访问HTML文件
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .resourceChain(false);
    }
}
